"""测试运行脚本"""

import sys
import pytest
from pathlib import Path

def run_tests():
    """运行所有测试"""
    # 获取项目根目录
    project_root = Path(__file__).parent
    
    # 设置测试参数
    test_args = [
        str(project_root / "tests"),  # 测试目录
        "-v",  # 详细输出
        "--tb=short",  # 简短的错误回溯
        "--strict-markers",  # 严格的标记模式
        "-x",  # 第一个失败后停止
        "--disable-warnings",  # 禁用警告
    ]
    
    print("开始运行文件归纳所测试套件...")
    print(f"测试目录: {project_root / 'tests'}")
    print("=" * 60)
    
    # 运行测试
    exit_code = pytest.main(test_args)
    
    if exit_code == 0:
        print("\n" + "=" * 60)
        print("✅ 所有测试通过！")
    else:
        print("\n" + "=" * 60)
        print("❌ 部分测试失败")
        print(f"退出代码: {exit_code}")
    
    return exit_code

def run_specific_test(test_pattern):
    """运行特定测试"""
    project_root = Path(__file__).parent
    
    test_args = [
        str(project_root / "tests"),
        "-v",
        "-k", test_pattern,  # 模式匹配
        "--tb=short",
        "--disable-warnings",
    ]
    
    print(f"运行匹配模式 '{test_pattern}' 的测试...")
    print("=" * 60)
    
    exit_code = pytest.main(test_args)
    return exit_code

if __name__ == "__main__":
    if len(sys.argv) > 1:
        # 运行特定测试
        pattern = sys.argv[1]
        exit_code = run_specific_test(pattern)
    else:
        # 运行所有测试
        exit_code = run_tests()
    
    sys.exit(exit_code)